package ve.usb.crm;

import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.cohesion.runtime.HibernateUtil;
import ve.usb.crm.hibernate.*;


/**
 *
 */
public class AccionesComentarios extends CohesionAction {

/**
     * Called by Struts for the execution of action ACrearDocumento.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions.
     * These exceptios will normally be treated with
     * the default exception action.
     */
    public ActionForward ACrearComentario(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreOportunidad", "APreTarea", "APreProyecto"};
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;
        final int SALIDA_2 = 2;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Comentario comentario = new Comentario();
            Personal personal = (Personal)request.getSession().getAttribute("Usuario");
            FCrearComentario fFCrearComentario = (FCrearComentario) form;


            comentario.setComentario(fFCrearComentario.getComentario());
            comentario.setFecha(new Date());
            comentario.setPersonal(personal);
            comentario.setAsociadoA(fFCrearComentario.getAsociadoA());
            comentario.setTablaAsociada((Categoria)s.load(Categoria.class, fFCrearComentario.getCategoria()));
            s.save(comentario);

            if(comentario.getTablaAsociada().getNombre().equals("Oportunidad")) {
                salida = SALIDA_0;
                request.setAttribute("idOportunidad", comentario.getAsociadoA());
            }
            else if(comentario.getTablaAsociada().getNombre().equals("Tarea")) {
                salida = SALIDA_1;
                request.setAttribute("idTarea", comentario.getAsociadoA());
            }
            else if(comentario.getTablaAsociada().getNombre().equals("Proyecto")) {
                salida = SALIDA_2;
                request.setAttribute("idProyecto", comentario.getAsociadoA());
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AEliminarDocumento.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions.
     * These exceptios will normally be treated with
     * the default exception action.
     */
    public ActionForward AEliminarComentario(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreHome",};
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idComentario = lookup_Long(request, "idComentario");

            Comentario comentario = (Comentario) s.load(Comentario.class, idComentario);
            s.delete(comentario);

            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                request.setAttribute("msg", "El comentario no pudo ser eliminado.");
            }
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

}